#import "IOSHelper_ios.h"
#import "IOSHelper.h"

static IOSHelper *g_iosHelper = nil;


IOSDevice CIOSHelper::CheckDeviceType()
{
    if ([[IOSHelper ShareInstance] CheckDeviceType:@"iPad"]) 
    {
        return IOS_DEVICE_IPAD;
    }    
    if ([[IOSHelper ShareInstance] CheckDeviceType:@"iPhone"]) 
    {
        return IOS_DEVICE_IPHONE;
    }
    
    if ([[IOSHelper ShareInstance] CheckDeviceType:@"iPod"]) 
    {
        return IOS_DEVICE_IPOD;
    }
    
    return IOS_DEVICE_UNKNOWN;
}

@implementation IOSHelper

+ (IOSHelper *)ShareInstance
{
    if(!g_iosHelper)
    {
        g_iosHelper = [[IOSHelper alloc] init];
    }
    
    return g_iosHelper;
}

- (void)OpenURL:(NSString *)url
{
    [[UIApplication sharedApplication] openURL : [NSURL URLWithString:url]];
}

- (BOOL)CheckDeviceType:(NSString *)name  
{
    NSString* deviceType = [UIDevice currentDevice].model;  
    NSLog(@"deviceType = %@", deviceType);  
    
    NSRange range = [deviceType rangeOfString:name];  
    return range.location != NSNotFound; 
}

- (NSString *)GetDeviceID
{
    NSLog(@"%@", [[UIDevice currentDevice] uniqueIdentifier]);
    return [[UIDevice currentDevice] uniqueIdentifier];
}

- (BOOL)CheckIsJailBroker
{
    BOOL ret = NO;
    
    NSString *cydiaPath = @"/Applications/Cydia.app";
    NSString *aptPath =   @"/private/var/lib/apt/";
    
    if([[NSFileManager defaultManager] fileExistsAtPath:cydiaPath])
    {
        ret = YES;
    }
    
    if([[NSFileManager defaultManager] fileExistsAtPath:aptPath])
    {
        ret = YES;
    }
    
    return ret;
}

- (BOOL)CheckIAPCraker
{
    if(![[IOSHelper ShareInstance] CheckIsJailBroker])
    {
        return NO;
    }
    
    NSString *ipaCrackerPath = @"/private/var/mobile/Library/SBSettings/Toggles/";
    
    
    if([[NSFileManager defaultManager] fileExistsAtPath:ipaCrackerPath])
    {
        UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",NULL) message:@"Your device exits iPACracker, you can't get the stuff!"   
                                                           delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];   
        [alerView show];   
        [alerView release]; 
        return YES;
    }
    
    return NO;
}

- (BOOL)CheckTimeOut
{
    NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    //[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    //[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    
    NSDate* firstDate = [dateFormatter dateFromString:@"2012-04-01"];
    NSDate* secondDate = [dateFormatter dateFromString:@"2012-05-01"];
    NSDate* todayData =  [NSDate date];
    
    NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate: firstDate];
    NSTimeInterval curDifference  = [todayData  timeIntervalSinceDate: firstDate];
    NSLog(@"%f", timeDifference);
    
    
    if(curDifference > timeDifference)
    {
        return NO;
    }
    return YES;
    
}
@end

